const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
    clean: true, // 每次构建前清理输出目录
    assetModuleFilename: "assets/[name].[contenthash][ext]", // 控制资源的输出路径
  },
  // devServer: {
  //   static: path.resolve(__dirname, "dist"), // 提供静态文件的目录
  //   hot: true, // 热更新
  //   open: ["starter.html"], // 自动打开浏览器
  //   port: 8080, // 开发端口
  // },
  devServer: {
    static: path.resolve(__dirname, "dist"),
    hot: true,
    open: ["/starter.html"],
    port: 8080,
    historyApiFallback: {
      disableDotRule: true,
      index: "/starter.html", // 确保这里设置正确
    },
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/i, // 图片资源处理
        type: "asset/resource", // Webpack 会处理并输出图片
      },
      {
        test: /\.(woff2?|eot|ttf|otf)$/i, // 字体文件处理
        type: "asset/resource",
      },
      {
        test: /src\/components\/.*\.html$/,
        use: "raw-loader",
      },
      {
        test: /\.html$/,
        exclude: /src\/components\//,
        loader: "html-loader",
        options: {
          sources: false,
        },
      },
    ],
  },
  plugins: [
    // 使用 CopyWebpackPlugin 插件来复制静态资源
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, "src/assets"), // 复制 src/assets 中的所有文件
          to: path.resolve(__dirname, "dist/assets"), // 复制到 dist/assets 目录
          noErrorOnMissing: true,
        },
      ],
    }),

    // 使用 HtmlWebpackPlugin 插件生成 HTML 文件

    new HtmlWebpackPlugin({
      template: "./src/pages/about.html", // 使用关于页面模板
      filename: "about.html", // 输出文件名
    }),
    new HtmlWebpackPlugin({
      template: "./src/pages/starter.html",
      filename: "starter.html",
    }),
  ],

  mode: "development", // 开发模式
};
//
